/**
 * Overlords of Chaos
 * 
 * http://www.theharlequin.com.au
 *
 */

var ooc = {
  
  canvas:    null,
  ctx:       null,
  overlords: [],  
  mousex:    0,  
  mousey:    0,  
  map:       null,  
  current_overlord: 0,
  draw_req:   true,
  sleep_lck:  false,
  sidebar:   null,
  bottombar: null,
  frames:    0,
  info_fps:     null,
  info_terrain: null,
  info_actions: null,
  
  onload: function() {
    /* Get a reference to the canvas element and 2d context */
    ooc.canvas = document.getElementById('ooc');
    ooc.ctx = ooc.canvas.getContext('2d');
    
    ooc.sidebar = document.getElementById('sidebar');
    ooc.bottombar = document.getElementById('bottombar');
    
    ooc.info_fps = document.getElementById('info_fps');
    ooc.info_terrain = document.getElementById('info_terrain');
    ooc.info_actions = document.getElementById('info_actions');
    
    /* Register event listeners */
    if (ooc.canvas.attachEvent) {
      ooc.canvas.attachEvent('onmousemove', ooc.onmousemove);
      ooc.canvas.attachEvent('onmouseout', ooc.onmouseout);
      ooc.canvas.attachEvent('onclick', ooc.onmouseclick);
      document.attachEvent('onkeydown', ooc.onkeypress);
    } else {
      ooc.canvas.addEventListener('mousemove', ooc.onmousemove, false);
      ooc.canvas.addEventListener('mouseout', ooc.onmouseout, false);
      ooc.canvas.addEventListener('click', ooc.onmouseclick, false);
      document.addEventListener('keydown', ooc.onkeypress, false);
    }
    
    
    // Create a test map for Alpha phase
    ooc.map = createtestmap();
    var cell = ooc.map.getCell(12, 12);
    ooc.overlords[0] = new Overlord('Overlord');
    ooc.overlords[0].setLocation(cell);
    cell.select();
    
    // Initialise the canvas size
    ooc.onresize();
    
    // Centre the view
    ooc.map.centre(cell);
    
    // Start the clocks...get set....GO!
    window.setInterval(ooc.oninterval, 20);
    window.setInterval(ooc.onfpsupdate, 1000);
  },

  onmouseclick: function(event) {
    ooc.map.selectCell(ooc.mousex, ooc.mousey);
  },

  onmousemove: function(event) {
    if (event.x) {
      ooc.mousex = event.x;
      ooc.mousey = event.y;
    } else {
      ooc.mousex = event.pageX - ooc.canvas.offsetLeft;
      ooc.mousey = event.pageY - ooc.canvas.offsetTop;
    }
    
    ooc.map.onmousemove();
  },

  onmouseout: function() {
    ooc.map.onmouseout();
  },

  onkeypress: function(event) {
    if(event.which)
      keynum = event.which;
    else
      keynum = event.keyCode;
    
    if (keynum == 38)
      ooc.map.moveSelected(0, -1);
  },
  
  onresize: function() {
    var width = ooc.sidebar.offsetLeft - 20;
    var height = ooc.bottombar.offsetTop - 20;
    
    if (width != ooc.canvas.width || height != ooc.canvas.height) {
        ooc.canvas.width = width;
        ooc.canvas.height = height;
    	ooc.map.onresize();
    }
    
  },
  
  onfpsupdate: function() {
    ooc.info_fps.innerHTML = ooc.frames;
    ooc.frames = 0;
  },
  
  currentOverlord: function() {
    return ooc.overlords[ooc.current_overlord];
  },

  usersturn: function() {
    return true;
  },

  redraw: function() {
    ooc.draw_req = true;
  },
  
  oninterval: function() {
    ooc.onresize();
    ooc.frames++;
    ooc.sleep_lck = !ooc.sleep_lck;
    if (ooc.draw_lck || !ooc.draw_req) return;
    ooc.draw_req = false;
    ooc.map.draw(ooc.ctx);
  }

};

window.onload = ooc.onload;
